package org.cable.common.config;

import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.BlockAttackInnerInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;

import javax.sql.DataSource;

/**
 * mybatis-plus的分页插件
 * @author hsj
 * @date 2023/11/6 11:40
 */
@Configuration
public class MybatisPlusConfig {

        @Bean
        public SqlSessionFactory sqlSessionFactory(DataSource dataSource) throws Exception
        {
            //使用MybatisPlus得使用“MybatisSqlSessionFactoryBean”，不然没法用内置方法
            MybatisSqlSessionFactoryBean sessionFactory = new MybatisSqlSessionFactoryBean();
            sessionFactory.setDataSource(dataSource);
            //注意这里，MybatisPlus需要引入拦截器才能使分页生效
            sessionFactory.setPlugins(mybatisPlusInterceptor());
            return sessionFactory.getObject();
        }

        /**
         * 分页
         *
         * @return
         */
        @Bean
        public MybatisPlusInterceptor mybatisPlusInterceptor() {
            MybatisPlusInterceptor mybatisPlusInterceptor = new MybatisPlusInterceptor();
            mybatisPlusInterceptor.addInnerInterceptor(new PaginationInnerInterceptor());
            mybatisPlusInterceptor.addInnerInterceptor(new BlockAttackInnerInterceptor());
            return mybatisPlusInterceptor;
        }


}
